package cn.krund.lyc.hotlerobot.receiver;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.SystemClock;
import android.util.Log;

import cn.krund.lyc.hotlerobot.activity.ui.GuideActivity;

/**
 * @包名: cn.krund.lyc.hotlerobot.receiver
 * @作者: 刘育成
 * @时间: 2018/3/27  9:45
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: WifiReceiver
 */

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)){//监听wifi信号强度的变化
                Log.i("----","wifi信号强度");
            }else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){//wifi连接上与否
                Log.i("----","网络状态改变");
                NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
                    Log.i("----","wifi网络连接断开");
                } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {

                    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                    //获取当前wifi名称
                    System.out.println("连接到网络 " + wifiInfo.getSSID());
                    Log.i("----","连接到网络"+ wifiInfo.getSSID());

                }
            }
            else if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {//wifi打开与否
                int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);

                if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
                    System.out.println("系统关闭wifi");
                    Log.i("----","系统关闭wifi");
                } else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
                    System.out.println("系统开启wifi");
                    Log.i("----","系统开启wifi:"+ SystemClock.elapsedRealtime());

                    SharedPreferences sp = context.getSharedPreferences("startapp",Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();

                    SystemClock.elapsedRealtime();
                    if (SystemClock.elapsedRealtime()>50000){
                        editor.putBoolean("isOpen",false);
                        editor.commit();
                    }else if (SystemClock.elapsedRealtime()<50000){
                        editor.putBoolean("isOpen",true);
                        editor.commit();
                    Intent it = new Intent(context,GuideActivity.class);
                    it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(it);
                    }

                }
            }
    }
}
